//
//  SearchViewController.m
//  GentlemanAnime
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "SearchViewController.h"
#import "AFNetworking.h"
#import "AFNetworkReachabilityManager.h"
#import "UIImageView+WebCache.h"
#import "SearchCollectionViewModel.h"
#import "SearchCollectionViewCell.h"
#import "SearchDetailViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SearchViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
// 做搜索栏
@property(nonatomic,retain)UISearchBar *AnimeSearchBar;
@property(nonatomic,retain)NSMutableArray *AnimeSearchArr;
@property(nonatomic,retain)UICollectionViewCell *SearchCollectionView;
@property(nonatomic,retain)NSMutableArray *SearchArr;
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)UITextField *searchTextField;
//搜索详情页面
@property(nonatomic,retain)NSMutableArray *SearchDetailArr;



@end

@implementation SearchViewController
- (void)dealloc
{
    [_SearchArr release];
    [_SearchCollectionView release];
    [_searchTextField release];
    [_collectionView release];
    [_AnimeSearchBar release];
    [_AnimeSearchArr release];
    [_SearchDetailArr release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.SearchArr = [NSMutableArray array];
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.AnimeSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 10, 30)];
    self.AnimeSearchBar.layer.borderColor = [UIColor whiteColor].CGColor;
    //在搜索栏上设置标题
    self.navigationItem.titleView = self.AnimeSearchBar;
    self.AnimeSearchBar.placeholder = @"漫画名、作者";
    self.AnimeSearchBar.clearsContextBeforeDrawing = YES;
  
    //后添加的代理人
    self.AnimeSearchBar.delegate = self;

    self.searchTextField = [[UITextField alloc]init];
    self.searchTextField = [[[self.AnimeSearchBar.subviews firstObject]subviews]lastObject];
   
    //设置背景颜色
    self.searchTextField.backgroundColor = [UIColor colorWithRed:62/255.0 green:118/255.0 blue:210/255.0 alpha:1];
    self.searchTextField.layer.cornerRadius = 10;
   

    //瀑布流放置热门搜索的
    UICollectionViewFlowLayout *HotFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置item的大小
    HotFlowLayout.itemSize = CGSizeMake(WIDTH / 3 - 20, 40);
    HotFlowLayout.minimumLineSpacing = 1;
    //设置滚动的方向
    HotFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置最小的列间距
    HotFlowLayout.minimumInteritemSpacing = 1;
    
    //设置左下右的边距
    HotFlowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 5, 10);
    
    //创建一个UICollectionView(高度-头和尾的高度113)
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:HotFlowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.contentInset = UIEdgeInsetsMake(WIDTH / 4, 0, 0, 0);
    [HotFlowLayout release];
    [_collectionView release];
    //注册
    [self.collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //热门搜索
    UILabel *HotSearchLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, -60, 80, 30)];
    HotSearchLabel.text = @"热门搜索:";
    [self.collectionView addSubview:HotSearchLabel];
    [HotSearchLabel release];
    
    [self createData];

}

//回收盘
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    return YES;
}
-(void)createData
{
    //通过AFN进行网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://api.dmzj.com/dynamic/comicsearchhot.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    self.SearchArr = [NSMutableArray array];
    NSDictionary *dic = responseObject;
    NSArray *arr = dic[@"data"];
    for (NSDictionary *tempDic in arr) {
        SearchCollectionViewModel *SearchModel = [[SearchCollectionViewModel alloc]init];
        [SearchModel setValuesForKeysWithDictionary:tempDic];
        [self.SearchArr addObject:SearchModel];
        [SearchModel release];
    }
    [self.collectionView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
    
 }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.SearchArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchCollectionViewCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    SearchCollectionViewModel *searchModel = self.SearchArr[indexPath.row];
    cell.nameLabel.text = searchModel.name;

    return cell;
}

#pragma maek collectionView的点击方法
//调转动漫的详情页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchDetailViewController *SearchDetailVC = [[SearchDetailViewController alloc]init];
    [self.navigationController pushViewController:SearchDetailVC animated:YES];
    SearchCollectionViewModel *searchModel = self.SearchArr[indexPath.row];
    SearchDetailVC.SearchDetailName = searchModel.name;
    //把ITem中的值传给searchBar
    self.AnimeSearchBar.text = searchModel.name;
    [SearchDetailVC release];
    
}

//搜索栏的点击事件
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.AnimeSearchBar resignFirstResponder];
    
    [self.AnimeSearchBar setKeyboardType:UIKeyboardTypeEmailAddress];
    //设置return键的样式
    self.AnimeSearchBar.returnKeyType = UIReturnKeyDone;
    //设置自动检索的类型
//    [self.AnimeSearchBar setAutocorrectionType:UITextAutocorrectionTypeDefault];
    
    SearchDetailViewController *SearchDetailVC = [[SearchDetailViewController alloc]init];
    [self.navigationController pushViewController:SearchDetailVC animated:YES];
    SearchDetailVC.SearchDetailName = self.AnimeSearchBar.text;
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
